package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.HealthPunchLog;
import com.ruoyi.system.domain.Student;
import com.ruoyi.system.service.IHealthPunchLogService;
import com.ruoyi.system.service.IStudentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * HealthPunchLogController
 *
 * @author ruoyi
 * @date 2023-01-08
 */
@Controller
@RequestMapping("/system/HealthPunchLog")
public class HealthPunchLogController extends BaseController {
    private String prefix = "system/HealthPunchLog";

    @Autowired
    private IHealthPunchLogService healthPunchLogService;
    @Autowired
    private IStudentService studentService;

    @RequiresPermissions("system:HealthPunchLog:view")
    @GetMapping()
    public String HealthPunchLog(ModelMap modelMap) {
        modelMap.put("sdudents", studentService.selectStudentList(new Student()));
        return prefix + "/HealthPunchLog";
    }

    /**
     * 查询HealthPunchLog列表
     */
    @RequiresPermissions("system:HealthPunchLog:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HealthPunchLog healthPunchLog) {
        startPage();
        String isPunched = healthPunchLog.getPunched();
        List<HealthPunchLog> list = new ArrayList<>(10);
        if ("2".equals(isPunched)) {
            list = healthPunchLogService.selectUnHealthPunchLogList(healthPunchLog);
        } else {
            list = healthPunchLogService.selectHealthPunchLogList(healthPunchLog);
        }
        return getDataTable(list);
    }

    /**
     * 导出HealthPunchLog列表
     */
    @RequiresPermissions("system:HealthPunchLog:export")
    @Log(title = "HealthPunchLog", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(HealthPunchLog healthPunchLog) {
        List<HealthPunchLog> list = healthPunchLogService.selectHealthPunchLogList(healthPunchLog);
        ExcelUtil<HealthPunchLog> util = new ExcelUtil<HealthPunchLog>(HealthPunchLog.class);
        return util.exportExcel(list, "HealthPunchLog数据");
    }

    /**
     * 新增HealthPunchLog
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        modelMap.put("sdudents", studentService.selectStudentList(new Student()));
        return prefix + "/add";
    }

    /**
     * 新增保存HealthPunchLog
     */
    @RequiresPermissions("system:HealthPunchLog:add")
    @Log(title = "HealthPunchLog", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(HealthPunchLog healthPunchLog) {
        return toAjax(healthPunchLogService.insertHealthPunchLog(healthPunchLog));
    }

    /**
     * 修改HealthPunchLog
     */
    @RequiresPermissions("system:HealthPunchLog:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        HealthPunchLog healthPunchLog = healthPunchLogService.selectHealthPunchLogById(id);
        mmap.put("healthPunchLog", healthPunchLog);
        mmap.put("sdudents", studentService.selectStudentList(new Student()));
        return prefix + "/edit";
    }

    /**
     * 修改保存HealthPunchLog
     */
    @RequiresPermissions("system:HealthPunchLog:edit")
    @Log(title = "HealthPunchLog", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(HealthPunchLog healthPunchLog) {
        return toAjax(healthPunchLogService.updateHealthPunchLog(healthPunchLog));
    }

    /**
     * 删除HealthPunchLog
     */
    @RequiresPermissions("system:HealthPunchLog:remove")
    @Log(title = "HealthPunchLog", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(healthPunchLogService.deleteHealthPunchLogByIds(ids));
    }

    /**
     * 统计软件学院当天健康打卡未完成的人数
     */
    @RequiresPermissions("system:HealthPunchLog:list")
    @PostMapping("/countNotHealthPunch")
    @ResponseBody
    public AjaxResult countNotHealthPunch() {
        return AjaxResult.success("操作成功", healthPunchLogService.countNotHealthPunchLog());
    }

    /**
     * 查看历史填报记录
     */
    @RequiresPermissions("system:HealthPunchLog:list")
    @PostMapping("/selectSelfHealthPunch")
    @ResponseBody
    public TableDataInfo selectSelfHealthPunch(String studentId) {
        startPage();
        List<HealthPunchLog> list = healthPunchLogService.selectHealthPunchLogByStudentId(studentId);
        return getDataTable(list);
    }
}
